package threadcoreknowledge.stopthreads.volatiledemo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 描述：     用中断来修复刚才的无尽等待问题
 */
public class WrongWayVolatileFixed {

    public static void main(String[] args) throws InterruptedException {
        WrongWayVolatileFixed body = new WrongWayVolatileFixed();
        ArrayBlockingQueue storage = new ArrayBlockingQueue(10);

        Producer producer = body.new Producer(storage);
        Thread producerThread = new Thread(producer,"threadProducer");
        producerThread.start();
        Thread.sleep(1000);

        Consumer consumer = body.new Consumer(storage);
        while (consumer.needMoreNums()) {
            System.out.println(consumer.storage.take() + "被消费了");
            Thread.sleep(100);
        }
        System.out.println("消费者不需要更多数据了.消费者线程main执行完毕了,马上对生产者进行interrupt");
        producerThread.interrupt();
    }


    class Producer implements Runnable {

        BlockingQueue storage;

        public Producer(BlockingQueue storage) {
            this.storage = storage;
        }


        @Override
        public void run() {
            int num = 0;

            // ---------------------  第一种方式: tryCatch位于while循环体内. 第一种方式,程序仍然无法正确停止 ----------------- 
            // --------------------- 第二种方式: tryCatch位于while循环体外. 第二种方式,程序可以正确停止 -----------
            runFirstWay(num);
//            runSecondWay(num);
        }


        public void runFirstWay(int num) {
            while (num <= 100000 && !Thread.currentThread().isInterrupted()) {
                try {
                    if (num % 100 == 0) {
                        System.out.println("第一种方式启动.num 111 is " + num+"---当前线程isInterrupted=" + Thread.currentThread().isInterrupted());
                        storage.put(num);
                        System.out.println("第一种方式启动.num 222 is " + num+"---当前线程isInterrupted=" + Thread.currentThread().isInterrupted());
                        System.out.println(num + "是100的倍数,被放到仓库中了.");
                    }
                    num++;
                } catch (InterruptedException e) {
                    System.out.println("remainingCapacity---"+storage.remainingCapacity());
                    System.out.println("----进入catch了----当前线程isInterrupted=" + Thread.currentThread().isInterrupted()+"-----storage.size="+storage.size());
                    e.printStackTrace();
                } finally {
//                    System.out.println("生产者结束运行");
                }
            }
        }

        public void runSecondWay(int num) {
            try {
                while (num <= 100000 && !Thread.currentThread().isInterrupted()) {
                    if (num % 100 == 0) {
                        System.out.println("第二种方式启动.num 111 is " + num);
                        storage.put(num);
                        System.out.println("第二种方式启动.num 222 is " + num);
                        System.out.println(num + "是100的倍数,被放到仓库中了.");
                    }
                    num++;
                }
            } catch (InterruptedException e) {
                System.out.println("-----进入catch了-----");
                e.printStackTrace();
            } finally {
                System.out.println("生产者结束运行");
            }
        }

    }


    class Consumer {

        BlockingQueue storage;

        public Consumer(BlockingQueue storage) {
            this.storage = storage;
        }

        public boolean needMoreNums() {
            double random = Math.random();
            if (random > 0.85) {
                System.out.println("random is "+random);
                return false;
            }
            return true;
        }
    }
}
